using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web;
using System.Collections;
using System.Data;

namespace Ent.Web.Helper
{
    public class FAQHelper
    {
        private static Dictionary<string, List<KeyValuePair<string, string>>> _FAQContent = null;
        static Dictionary<string, List<KeyValuePair<string, string>>> FAQContent
        {
            get
            {
                if (_FAQContent == null)
                    Ent.Web.Helper.Priviledge.Init(ConfigurationManager.AppSettings["FAQ"]);
                return _FAQContent;
            }
        }
        public static void Init(string path)
        {
            path = HttpRuntime.AppDomainAppPath + "/" + path;
            if (_FAQContent == null)
            {
                _FAQContent = new Dictionary<string, List<KeyValuePair<string, string>>>();
                XMLHelper _xml = new XMLHelper(path);
                string root = "//Application/FAQ";
                string[] names = _xml.GetAttributesOfAllNodes(root, "name");
                foreach (string name in names)
                {
                    string xpath = root + "[@name='" + name + "']";
                    List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
                    DataTable dt = _xml.GetChildNodesAsDataTable(xpath, "Content", new string[] { "type", "<innertext>" });
                    foreach (DataRow dr in dt.Rows)
                    {
                        list.Add(new KeyValuePair<string, string>(dr["type"].ToString(), dr["innertext"].ToString()));
                    }
                    _FAQContent.Add(name.ToLower(), list);
                }
            }
        }
        public static string[] FAQs
        {
            get {
                string[] retValue = new string[FAQContent.Count];
                FAQContent.Keys.CopyTo(retValue, 0);
                return retValue;
            }
        }
        public static List<KeyValuePair<string, string>> GetFAQContent(string contentName)
        {
            if (FAQContent.ContainsKey(contentName.ToLower()))
                return FAQContent[contentName.ToLower()];
            else return new List<KeyValuePair<string, string>>();
        }
    }
}
